<template>
  <view>
    <u-modal v-model="show" :content="content"></u-modal>
    <view class="choose-btn">
      <u-button @click="postData" type="primary" :ripple="true" size="medium">选择文件</u-button>
    </view>
  </view>
</template>

<script>
import {getUrl} from "../../utils/constants";
import { getToken } from "../../common/auth";

export default {
  data() {
    return {
      show:false,
      content: '上传资料请进入网页版',
      id: '',
      url: getUrl() + "/api/data-file/save"
    };
  },
  onLoad(e){
    this.id = e.id;
  },
  methods: {
    postData(){
      //#ifndef H5
      this.show = true;
      // #endif
      //#ifdef H5
      uni.chooseFile({
        count: 5, //默认100
        extension:['.doc','.xlsx','.docx',".ppt",".pptx",".xls"],
        success: (res) => {
          this.upload(res.tempFiles);
        },
        fail: (err) => {
          console.log("出错信息",err)
          this.$u.toast("选择文件出错！")
        }
      });
      //#endif

    },
    upload(tempFiles){
      for (let item of tempFiles) {
        console.log(item);
        if (item.size < 70 * 1024 * 1024){
          console.log(item.path)
          uni.uploadFile({
            url: this.url, //仅为示例，非真实的接口地址
            filePath: item.path,
            header: {
              "token": getToken(),
            },
            name: 'file',
            formData: {
              "courseId": this.id
            },
            success: (res) => {
              console.log(res);
            }
          });
        }else {
          this.$u.toast( item.name + "已超出规定大小，建议拆分为两个上传")
        }
      }
    }
  }
}
</script>

<style lang="scss">
  .choose-btn{
    text-align: center;
    margin-top: 40rpx;
  }
</style>
